package tx.audioAndVideoRecorder;


import java.io.File;
import java.util.ArrayList;

import android.app.ListActivity;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

//public class AudioActivity extends Activity{
public class AudioActivity extends ListActivity{
    
	private static final String TAG = "AudioActivity";
	private MyAudioRecorder recorder = null;  
	private MyAccessToFiles accessFile = null;
	private MediaPlayer player = null;
	
	private ListView listV;
	private String fileSelected = null; 
	
	private Button mbtnPlay = null;
	
	// Variable relative to the popup window
	private PopupWindow popUp;
	private View popupLayout = null;
	private boolean click = true;
	
	// List of audio row
	private ArrayList<audio> audioFiles = null;
	// Custom adapter for audio row
	private audioAdapter m_adapter = null;
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        
        recorder = new MyAudioRecorder();
        accessFile = new MyAccessToFiles();
        
	    audioFiles = myUtil.convertStringToAudio(accessFile.getAudioFiles());
	    
	    m_adapter = new audioAdapter(this, R.layout.audio_listview_layout, audioFiles);
	    
	    //Popup initialisation
	    //TODO : choisir ou on met la popup window : Dans la liste view ou dans la fenetre principale
	    popUp = new PopupWindow(this);
	    popupLayout = getLayoutInflater().inflate(R.layout.popup_audio, null);
		popUp.setContentView(popupLayout);
        
		// Get design from tab_audio.xml
        View header = getLayoutInflater().inflate(R.layout.tab_audio, null);
        listV = getListView();
        
        // Allow to add button before the list view
        listV.addHeaderView(header);
        
        // Set Audio adapter to the listActivity
        setListAdapter(m_adapter);
        
        // Set an action listener on the list
        listV.setOnItemClickListener(listAudioListener);
        
        setButtonHandlers();
	    enableButtons(false); 
	              
    }
	
	
	
	/*
	 * Add action listeners on button here
	 * 
	 */
    private void setButtonHandlers() {
            ((Button)findViewById(R.id.btnStart)).setOnClickListener(btnClick);
		    ((Button)findViewById(R.id.btnStop)).setOnClickListener(btnClick);
		    ((Button)findViewById(R.id.btnFormat)).setOnClickListener(btnClick);
		    ((Button)findViewById(R.id.btnReplay)).setOnClickListener(btnClick);
    }
    
    private void enableButton(int id,boolean isEnable){
        	((Button)findViewById(id)).setEnabled(isEnable);
		}
	
	private void enableButtons(boolean isRecording) {
	        enableButton(R.id.btnStart,!isRecording);
	        enableButton(R.id.btnFormat,!isRecording);
	        enableButton(R.id.btnStop,isRecording);
			}
	
	
	
	private View.OnClickListener btnClick = new View.OnClickListener() {
	        @Override
	        public void onClick(View v) {
	                switch(v.getId()){
	                        case R.id.btnStart:{
	                        		// Start button has been clicked	                        	
	                        		enableButtons(true);
	                                Toast.makeText(getApplicationContext(), "START RECORD", 1000).show();
	                                // Start recording audio
	                                recorder.startRecording();
	                                                
	                                break;
	                        }
	                        case R.id.btnStop:{	                        		
	                        		// Stop button has been clicked
	                        	  	enableButtons(false);
	                        		Toast.makeText(getApplicationContext(), "STOP RECORD", 1000).show();
	                                
	                        		// stop recording audio
	                                recorder.stopRecording();
	                               
	                                // Get the last file created
	                                String lastFile = recorder.getsLastRecorderFile();
	                                audio temp = new audio(lastFile);
	                                // Add the new file to the current list adapter
	                                m_adapter.add(temp);
	                                // Notify to the custom adapter that dataSet has changed
	                                m_adapter.notifyDataSetInvalidated();
	                                m_adapter.notifyDataSetChanged();
	                                
	                                break;
	                        }
	                        case R.id.btnReplay:{
	                        		                        	
	                        	// TODO add a button to replay and a different one to play                    		                        	
	                        	// Replay the recorded sound
	                        	try {
	                        		
	                        		if(fileSelected != null)
	                        		{
	                        			File lastRecordedfile = new File(fileSelected);
		                        		player = MediaPlayer.create(getApplicationContext(),Uri.fromFile(lastRecordedfile));
		                        		player.start();	
		                        		Toast.makeText(getApplicationContext(), "REPLAY", 1000).show();
	                        		}
	                        		else
	                        		{
	                        			Toast.makeText(getApplicationContext(), "NO FILE SELECTED", 1000).show();
	                        		}
	                        		
								} catch (Exception e) {
									Log.e(TAG,e.getMessage());
	                    			e.printStackTrace();
								}
	                        	
	                        	
	                        	 
	                            break;
	                    }
	                }
	        }
	}; 
	
	
	private OnItemClickListener listAudioListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			// Get which row has been clicked (arg3) 
			audio temp = (audio)listV.getItemAtPosition((int)arg3+1);
			String filePlay = temp.getAudioFileName();
			
			Toast.makeText(getApplicationContext(), filePlay, 1000).show();
			if (click) {
			 
				// Display the popupWindow
       	     	popUp.showAtLocation(popupLayout, Gravity.CENTER, 10, 10);
       	     	popUp.update(50, 50, 300, 80);
       	     	TextView txtTemp = (TextView)popupLayout.findViewById(R.id.lblPopup);
       	     	txtTemp.setText(filePlay);
       	     
       	     	File lastRecordedfile = new File(filePlay);
       	     	// Play the file selected
       	     	player = MediaPlayer.create(getApplicationContext(),Uri.fromFile(lastRecordedfile));
       	     	player.start();	
    		 
	    		 // To dismiss the popupWindow
	    		 //popUp.dismiss();
	    		 /*click = false;
	       	    } else {
	       	     popUp.dismiss();
	       	     click = true;*/
       	    }
		}
	};
}
